﻿using System;

namespace Benecura.Interfaces.Web.Models
{
    public class ScheduleWeek
    {
        #region Fields
        private ScheduleDay[] _weekDays = new ScheduleDay[7];
        #endregion
        #region Properties
        public ScheduleDay[] WeekDays { get { return _weekDays; } }
        public string DateRange
        {
            get
            {
                //Week day span across 2 months
                if (_weekDays[0].Date.Month < _weekDays[6].Date.Month)
                {
                    return string.Format("{0} - {1}",
                        _weekDays[0].Date.ToString("dd/MM"),
                        _weekDays[6].Date.ToString("dd/MM"));
                }
                //Week days in the same month
                else
                {
                    return string.Format("{0} - {1} {2}",
                        _weekDays[0].Date.ToString("dd"),
                        _weekDays[6].Date.ToString("dd"),
                        _weekDays[0].Date.ToString("MMM"));
                }
            }
        }
        #endregion
        #region Constructors
        public ScheduleWeek(int diff)
        {
            //Get today
            DateTime selectedDate = DateTime.Today.AddDays(7 * diff);
            //Get today day of week as Monday is the week start
            int dayOfWeek = (int)selectedDate.DayOfWeek - 1;
            //Sunday
            if (dayOfWeek == -1) dayOfWeek = 6;
            //Populate today
            _weekDays[dayOfWeek] = new ScheduleDay(selectedDate);
            //Populate the rest
            for (int i = 0; i <= 6; i++)
            {
                if (i != dayOfWeek)
                {
                    _weekDays[i] = new ScheduleDay(selectedDate.AddDays(i - dayOfWeek));
                }
            }
        }
        #endregion
    }
}